package com.design.marketPrj.application.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.design.marketPrj.application.bean.VO.ProductDetailVO;
import com.design.marketPrj.application.bean.layuiJSON;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ExcludeDataService {

    public layuiJSON getAllProductData(int page,int limit,String code) {
        JSONArray resultArrayData = new JSONArray();
        CloseableHttpClient client = HttpClients.createDefault();
        // 要调用的接口方法
//        String url = "https://api.doctorxiong.club/v1/fund/all";
        JSONObject jsonObject = null;
        try {

            URIBuilder uriBuilder = new URIBuilder("https://api.doctorxiong.club/v1/fund/all");
            HttpGet get = new HttpGet(uriBuilder.build());
            get.addHeader("content-type", "text/xml");
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(res.getEntity());// 返回json格式：
                jsonObject = JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONArray jsonArray = (JSONArray) jsonObject.get("data");
        for(int i=(page-1)*limit;i<(page)*limit&&i<jsonArray.size();i++)
        {
            JSONArray b = (JSONArray) jsonArray.get(i);
            ProductDetailVO productDetailVO = new ProductDetailVO();
            productDetailVO.setCode((String) b.get(0));
                if(StringUtils.isNotBlank(code) && !productDetailVO.getCode().equals(code)){
                    continue;
                }
            productDetailVO.setAbbreviation((String) b.get(1));
            productDetailVO.setName((String) b.get(2));
            productDetailVO.setType((String) b.get(3));
            productDetailVO.setFullName((String) b.get(4));
            JSONObject jsonObject1= (JSONObject)JSON.toJSON(productDetailVO);
            resultArrayData.add(jsonObject1);
        }
        layuiJSON layuiJSON = new layuiJSON();
        layuiJSON.setData(resultArrayData);
        layuiJSON.setCount(jsonArray.size());
        layuiJSON.setCode(0);
        layuiJSON.setMsg("");

        return layuiJSON;
    }

    public JSONObject getProductDetail(String code,String startDate) {



        HttpGet get = null;
        try {
            //创建URIBuilder组装url
            URIBuilder uriBuilder = new URIBuilder("https://api.doctorxiong.club/v1/fund/detail");
            if(StringUtils.isNotBlank(code))
                uriBuilder.setParameter("code",code);
            if(StringUtils.isNotBlank(startDate))
                uriBuilder.setParameter("startDate",startDate);
            //创建Httpget对象
            get = new HttpGet(uriBuilder.build());
        }
        catch (Exception e){
            e.printStackTrace();
        }



        //get请求api数据
        CloseableHttpClient client = HttpClients.createDefault();
        String result = null;
        try {
            get.addHeader("content-type", "text/xml");
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());// 返回json格式：
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //解析json字符串
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONObject dataObject = (JSONObject) jsonObject.get("data");

        return dataObject;
    }

    //获取某用户下所有基金对应的当日净值
    public Map getCurrentWorth(List<String>list){
        String concatString="";
        for (String string : list){
            concatString += string;
            if(list.size() - 1 != list.indexOf(string)){
                concatString += ",";
            }
        }
        HttpGet get = null;
        try {
            //创建URIBuilder组装url
            URIBuilder uriBuilder = new URIBuilder("https://api.doctorxiong.club/v1/fund");
            if(StringUtils.isNotBlank(concatString))
                uriBuilder.setParameter("code",concatString);
            //创建Httpget对象
            get = new HttpGet(uriBuilder.build());
        }
        catch (Exception e){
            e.printStackTrace();
        }



        //get请求api数据
        CloseableHttpClient client = HttpClients.createDefault();
        String result = null;
        try {
            get.addHeader("content-type", "text/xml");
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());// 返回json格式：
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray dataArray = (JSONArray) jsonObject.get("data");

        Map codeToWorth = new HashMap();
        for(Object dataObject : dataArray){
            JSONObject object = (JSONObject)dataObject;
            codeToWorth.put(object.get("code"),object.get("netWorth"));
        }

        return codeToWorth;
    }

}
